package com.wxx.example.callback;

import com.wxx.sdk.wechat.WXPayUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/weChatCallBack")
public class WeChatCallBackController {



    /**
     * 微信回调案例
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/test", produces = "text/html;charset=utf-8")
    public String test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        boolean bool = false;
        Map<String, String> return_data = new HashMap<String, String>();

        ServletInputStream instream = request.getInputStream();
        StringBuffer sb = new StringBuffer();
        int len = -1;
        byte[] buffer = new byte[1024];

        while ((len = instream.read(buffer)) != -1) {
            sb.append(new String(buffer, 0, len));
        }
        instream.close();

        //将微信返回的数据解析成map
        //map返回的参数案例
        //        {
        //           "appid": "wx4c36afc61bb54516",
        //           "bank_type": "CFT",
        //           "cash_fee": "1",
        //           "fee_type": "CNY",
        //           "is_subscribe": "N",
        //           "mch_id": "1503698631",
        //           "nonce_str": "FE18C39734C0496EA5B4A302C73D734F",
        //           "openid": "oiKat0ecjo0ZcrxNSmLr5LjMSywI",
        //           "out_trade_no": "201807021036198134",
        //           "result_code": "SUCCESS",
        //           "return_code": "SUCCESS",
        //           "sign": "3EFEBE2558C41D82DF6D267CA87E3644",
        //           "time_end": "20180702103627",
        //           "total_fee": "1",  支付金额
        //           "trade_type": "APP",
        //           "transaction_id": "4200000131201807026102676889"
        //        }
        Map<String, String> map = WXPayUtil.xmlToMap(sb.toString());
        //业务结果 result_code SUCCESS
        //此字段是通信标识，非交易标识，交易是否成功需要查看result_code来判断 SUCCESS/FAIL
        if ("SUCCESS".equals(map.get("return_code")) && "SUCCESS".equals(map.get("result_code"))) {
            //商户生成的订单号 自己生成的
            String out_trade_no = map.get("out_trade_no");
            //业务处理


            //业务处理完，将boolean bool =true;
            bool = true;
            if (bool) {
                //修改订单成功之后通知微信，防止微信重复调用
                return_data.put("return_code", "SUCCESS");
                return_data.put("return_msg", "OK");
                return WXPayUtil.mapToXml(return_data);
            } else {
                return_data.put("return_code", "FAIL");
                return WXPayUtil.mapToXml(return_data);
            }

        } else {
            //修改订单成功之后通知微信，防止微信重复调用
            return_data.put("return_code", "FAIL");
            return WXPayUtil.mapToXml(return_data);
        }
    }

}
